//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef MAINWND_H_
#define MAINWND_H_

#include "def.h"
#include <wx/aui/aui.h>
#include <wx/taskbar.h>
#include "configdata.h"
#include "guilogic.h"
#include "propertystorage.h"
#include "helppanel.h"

class CMainWnd: public wxFrame/*, public wxTaskBarIcon*/
{
public:
									CMainWnd(CConfigData& config, const wxString& title, const wxPoint& pos, const wxSize& size);
	virtual							~CMainWnd();
	
	void							StartUp();

	CPropertyStorage&				GetPropStorage() { return m_propStorage; }
	CGUILogic&						GetLogic()		 { return m_guilogic;	 }
	CConfigData&					GetConfig()		 { return m_config;		 }
	
	void							Refresh();
	void							InitRouterConnection();
	
	void							OnPopUpMenu(const wxPoint& pt, wxWindow* pParent );

	void							SetPrgTitle(const wxString& str);
	void							ShowConnectionWizard();
	void							ShowConfig(PANELTYPE pl = cPTNone);
	void							UpdateHelpPage();

	void							RemoveTab( cCmdCode cmd );

	void							GenerateStatusLog();
	void							OnRouterConnected();
	
	DECLARE_EVENT_TABLE()
	
protected:
	//Menu
	void							OnQuit(wxCommandEvent& event);

	//File
	void							OnSave(wxCommandEvent& event);
	void							OnSaveUI(wxUpdateUIEvent& event);

	//View
	void							OnRefresh(wxCommandEvent& event);
	void							OnUpdateSpeed(wxCommandEvent& event);
	void							OnUpdateSpeedUpdateUI(wxUpdateUIEvent& event);
	void							OnViewLOG(wxCommandEvent& event);
	void							OnViewLOGUpdateUI(wxUpdateUIEvent& event);
	void							OnClear(wxCommandEvent& event);
	void							OnClearUI(wxUpdateUIEvent& event);
	void							OnHelpPanel(wxCommandEvent& event);
	void							OnHelpPanelUI(wxUpdateUIEvent& event);
	void							OnTimeSelector(wxCommandEvent& event);
	void							OnTimeSelectorUI(wxUpdateUIEvent& event);


	//Tools
	void							OnRestartRT(wxCommandEvent& event);
	void							OnRestartRTUI(wxUpdateUIEvent& event);
	void							OnConWizard(wxCommandEvent& event);
	void							OnOptions(wxCommandEvent& event);
	
	//Help
	void							OnHelp(wxCommandEvent& event);
	void							OnHelpTranslate(wxCommandEvent& event);
	void							OnAbout(wxCommandEvent& event);
	void							OnUpdateCheck(wxCommandEvent& event);
	void							OnDonation(wxCommandEvent& event);

	void							OnCompatibReport(wxCommandEvent& event);
	void							OnCompatibReportUI(wxUpdateUIEvent& event);
	
	//Custom
	void 							OnProcessThreadExit(wxCommandEvent& event);
	void 							OnProcessRouterData(wxCommandEvent& event);
	void							OnNewLogMsg(wxCommandEvent& event);
	
	
	//PopUP
	void 							OnConfig(wxCommandEvent& event);
	void							OnRefreshOne(wxCommandEvent& event);
	
	
	void							OnIconize(wxIconizeEvent& event);
	void							OnTabPageChanged(class wxAuiNotebookEvent& ev);
	void							OnPanelClose(wxAuiManagerEvent& ev);
	void							OnHelpPressed(wxHelpEvent& event);

	//Toolbar
	void							OnOptimizationSelector(wxCommandEvent& event);
	void							OnOptimizationSelectorUI(wxUpdateUIEvent& event);
	void							OnAdvanced(wxCommandEvent& event);
	void							OnAdvancedUpdateUI(wxUpdateUIEvent& event);

private:
	void							LoadSettings();
	void							InitMenu();
	void							InitToolBar();
	void							InitWorkArea();

	class wxWindow*					CreatePerfomanceTab();
	class wxWindow*					CreateConnectionsTab();
	class wxWindow*					CreateSysLogTab();
	class wxWindow*					CreateStatTab();
	class wxWindow*					CreateUsersTab();
	class wxWindow*					CreateIPSettingsTab();
	class wxWindow*					CreatePortForwardTab();
	class wxWindow*					CreateADSLTab();
	class wxWindow*					CreateISPTab();
	class wxWindow*					CreateWirelessTab();
	class wxWindow*					CreatePingTab();


	void							CheckConnectionParams(CONPARAMS& params);
	void							ShowConnectionWizard(CONPARAMS& params);
	void							DoUpdateCheck(bool bSilent=false);
	bool							IsVersionChanged(const std::string& strVer );
	
	
	
private:
     wxAuiManager					m_mgr;
     CConfigData&					m_config;
     CGUILogic						m_guilogic;
     CPropertyStorage				m_propStorage;
     PANELTYPE						m_plCurType;
     class wxAuiNotebook*			m_pBook;
     
	 wxWindow*						m_pPerfomance;
	 wxWindow*						m_pConnections;
	 wxWindow*						m_pBandwidth;
	 wxWindow*						m_pAccess;
	 wxWindow*						m_pPortForward;
	 wxWindow*						m_pIPSettings;
	 wxWindow*						m_pADSL;
	 wxWindow*						m_pWireless;
	 wxWindow*						m_pPing;

	 wxWindow*						m_pSysLog;
	 wxString						m_strTitle;
	 CHelpPanel*					m_pHelpPanel;
	 wxCriticalSection				m_critical;
	 volatile bool					m_bInside;
	 
	 struct	CRecursvGuard
	 {
		 CRecursvGuard( volatile bool& bInside ):m_bInside(bInside)
		 {
			 m_bInside = true;
		 }
		 
		 ~CRecursvGuard()
		 {
			 m_bInside = false;
		 }
		 
	 	private:
	 		volatile bool& m_bInside;
	 };
};

#endif /*MAINWND_H_*/
